<?php
/**
 * The controller to manage information of sale program.
 *
 */
$loader->requireOnce('ordo/ServiceGroup.class.php');
$loader->requireOnce('includes/Utils.class.php');
$loader->requireOnce('includes/AjaxRender/TicketRender.class.php');

class C_ServiceGroup extends Controller {
	
	var $template_mod;
	
	function C_ServiceGroup($template_mod = "general") {
		parent::Controller();
		if (Celini::getCurrentController() == 'ServiceGroup') {
			$menu =& Menu::getInstance();
			$menu->currentSection = 'admin';
		}
		$this->template_mod = $template_mod;
		$this->setLayout('admin');
		$this->view->path = 'service_group';
	}
	
	public function actionManagement() {
		return $this->view->render('management.html');
	}
	public function actionServiceGroupListAjax(){
		$serviceGroupList = Celini::newORDO('ServiceGroup')->listAll();
		$this->assign('serviceGroupList', $serviceGroupList);
		return $this->view->render('service_group_list.html');
	}
	/**
	 * Add or update a service group.
	 *
 	*/
	public function actionSaveServiceGroupAjax(){
		$serviceGroup 	= 	Celini::newORDO('ServiceGroup');
		$name			=	Utils::sanitizeText($_POST['name']);
		if (isset($_POST['service_group_id']) && $_POST['service_group_id'] > 0) {
            $serviceGroup->setup($_POST['service_group_id']);
       	}
       	$id	=	$serviceGroup->get_id();
       	if($id > 0){
       		$field	=	L10nHelper::getLocale();
       		$field	=	L10nHelper::changeField($field, "name");
			$data[$field]	=	$name;
       	}
       	else{
       		$supported	=	L10nHelper::getSupportedLocales();
			foreach ($supported as $locale	=>	$country){
				$locale	=	L10nHelper::changeField($locale, "name");
				$data[$locale]	=	$name;
			}
       	}
       
	   	$serviceGroup->populateArray($data);
	   	$serviceGroup->persist();
	}
	/**
	 * Check duplicate service group.
	 *
 	*/
	public function actionCheckServiceGroupNameAjax(){
		$serviceGroup = Celini::newORDO('ServiceGroup');
		$serviceGroupId = $_GET['service_group_id'];
//		var_dump($serviceGroupId);
		return $serviceGroup->checkName(trim($_GET['name']), $serviceGroupId);
	}
	
	public function actionMapServiceGroupAjax(){
		$serviceGroupList = Celini::newORDO('ServiceGroup')->listAll();
		$this->assign('serviceGroupList', $serviceGroupList);
		return $this->view->render('map_service_group.html');
	}
	
	/**
	 * Get mapped services and unmapped services of service group.
	 *
 	*/
	public function actionGetServicesAjax(){
		if($_GET['groupId'] == "" ){
			return 0;
		}
		$mappedServices = Celini::newORDO('Service')->getMappedServices($_GET['groupId']);
		$unmappedServices = Celini::newORDO('Service')->getUnmappedServices($_GET['groupId'],$type);
		
		$response = '<table>
					  
						<tr>
						  <td class="label">' . L10nHelper::_("Services are not in group yet") . '</th>
						  <td>&nbsp;</th>
					      <td class="label">' . L10nHelper::_("Services are in group") . '</th>
						</tr>
					  
					  
					    <tr>
		    			  <td class="field">
		        			<select id="unmappedServiceList" multiple="true" size="20" style="width: 250px">';
		foreach ($unmappedServices as $unmappedService){
        	$response .=   '<option value="'.$unmappedService['service_id'].'">'.$unmappedService['name'].'</option>';
        }
        $response .=       '</select>
		    		      </td>
		    		      <td style="vertical-align: top">
		        		    <input type="button" class="button_link" value=">>" onclick="javascript:moveItem('."'add'".')">
                	        <br/><br/>
                	        <br/><br/>
                            <input type="button" class="button_link" value="<<" onclick="javascript:moveItem('."'remove'".')">
                          </td>
                          <td class ="field">
            	            <select id="mappedServiceList" multiple="true" size="20" style="width: 250px">';
		foreach ($mappedServices as $mappedService){
        	$response .=   '<option value="'.$mappedService['service_id'].'">'.$mappedService['name'].'</option>';
        }
        $response .=      '</td>
		                 </tr>
		               
	                </table>';
        return $response;
	}
	
	public function actionGetServicesByIdAjax(){
		if($_GET['groupId'] == "" ){
			return '';
			
		}
		
		$services = Celini::newORDO('Service')->getServices($_GET['groupId']);
		
		$response = '
						<td class="label">' . L10nHelper::_("Service") . '</td>
					    <td class="field" colspan="2">
		        			<select id="serviceList" multiple="true" size="5" style="width: 400px;">';
		
								foreach ($services as $service){
						        	$response .=   '<option value="'.$service['service_id'].'">'.$service['name'].'</option>';
						        }
        $response .=       '</select>
						</td>
		    		      ';
        return $response;
	}
	
	/**
	 * Add or remove services of service group.
	 *
 	*/
	public function actionAddToServiceGroupAjax(){
		$service = Celini::newORDO('Service');
		$SeviceIds = split(",", $_POST['selectedSeviceIds']);
		$option =  trim($_POST['option']);
		if(strcmp  ( $option , "remove") == 0) {
			$groupId = 0;
		}else{
			$groupId = $_POST['groupId'];
		}
		$service->AddToServiceGroup($groupId, $SeviceIds );
	}
	
	public function actionMapServiceRoleAjax(){
		$serviceGroupList = Celini::newORDO('ServiceGroup')->listAll();
		$this->assign('serviceGroupList', $serviceGroupList);
		return $this->view->render('map_service_role.html');
	}
	
public function actionGetServiceRoleAjax(){
		if($_GET['groupId'] == "" ){
			return 0;
		}

		$assignedServices = Celini::newORDO('Service')->getAssignedServices();
		$notAssignedServices = Celini::newORDO('Service')->getNotAssignedServices($_GET['groupId']);
		
		$response = '<table>
					  
						<tr>
						  <td class="label">' . L10nHelper::_("Services are not calculated to follow roles") . '</th>
						  <td>&nbsp;</th>
					      <td class="label">' . L10nHelper::_("Services are calculated to follow roles") . '</th>
						</tr>
					  
					  
					    <tr>
		    			  <td class="field">
		        			<select id="notAssignedServiceList" multiple="true" size="20" style="width: 350px">';
		foreach ($notAssignedServices as $service){
        	$response .=   '<option value="'.$service['service_id'].'">'.$service['name'].'</option>';
        }
        $response .=       '</select>
		    		      </td>
		    		      <td style="vertical-align: top">
		        		    <input type="button" class="button_link" value=">>" onclick="javascript:moveRole('."'add'".')">
                	        <br/><br/>
                	        <br/><br/>
                            <input type="button" class="button_link" value="<<" onclick="javascript:moveRole('."'remove'".')">
                          </td>
                          <td class ="field">
            	            <select id="assignedServiceList" multiple="true" size="20" style="width: 250px">';
		foreach ($assignedServices as $service){
        	$response .=   '<option value="'.$service['service_id'].'">'.$service['name'].'</option>';
        }
        $response .=      '</td>
		                 </tr>
		               
	                </table>';
        return $response;
	}
	
	public function actionAddRoleToServiceAjax(){
		$service = Celini::newORDO('Service');
		$serviceIds = split(",", $_POST['selectedSeviceIds']);
		$option =  trim($_POST['option']);
		$response = '';
		if(strcmp  ( $option , "remove") == 0) {
			$service->RemoveRoleToService($serviceIds);
			$notAssignedServices = Celini::newORDO('Service')->getNotAssignedServices($_POST['groupId']);
			if(isset($notAssignedServices) && $notAssignedServices !=''){
				foreach ($notAssignedServices as $service){
        			$response .=   '<option value="'.$service['service_id'].'">'.$service['name'].'</option>';
        		}
			}
		}else{
			$service->AddRoleToService($serviceIds);
		}
		return $response;
	}
}
?>